package com.aspire.nm.component.miniServerDemo.clientTest;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import com.aspire.nm.component.commonUtil.httpClient.HttpTookit;
import com.aspire.nm.component.miniServerDemo.Main;



public class ServerTest_expensive {

/**
 * 
serverPort=7070
workQueueSize=500
maximumPoolSize=1000
corePoolSize=1000    
 */
    private AtomicInteger count_504=new AtomicInteger(0);
    private AtomicInteger count_503=new AtomicInteger(0);
    private AtomicInteger count_other=new AtomicInteger(0);
    private AtomicInteger count_200=new AtomicInteger(0);
    private AtomicInteger count_clientTimeout=new AtomicInteger(0);
    private AtomicInteger count_clientConnException=new AtomicInteger(0);
    private AtomicInteger count=new AtomicInteger(0);
    private int threadNum = 1000;
    private int countPerThread = 100;
    
    public static void main(String [] args) throws InterruptedException, InstantiationException, IllegalAccessException{
        new ServerTest_expensive().test();
    }
    public void test() throws InterruptedException, InstantiationException, IllegalAccessException{
        
        for(int i = 0;i < threadNum;i++){
            new Thread(){
                HttpTookit httpTookit = new HttpTookit();
                public void run(){
                    for(int j = 0 ;j < countPerThread ; j ++){
                        try{
                            httpTookit.doGet("http://"+Main.serverIp4Test+":7071/demo/expensive", null);
                            count_200.incrementAndGet();
                        }catch(RuntimeException e){
                            if(e.getMessage().endsWith("504")){
                                count_504.incrementAndGet();
                            }else if(e.getMessage().endsWith("503")){
                                count_503.incrementAndGet();
                            }else{
                                count_other.incrementAndGet();
                            }
                        }catch(java.net.SocketTimeoutException e1){
                            //并发稿，server的workQueueSize值高，可能会导致socket无法在服务端得到及时处理，会引发client socketTimeOut
                            count_clientTimeout.incrementAndGet();
                        }catch(java.net.ConnectException e2){
                          //并发稿，server的workQueueSize值低，
                            count_clientConnException.incrementAndGet();
                        }
                        catch (Throwable e) {
                            e.printStackTrace();
                            count_other.incrementAndGet();
                        } finally{
                            count.incrementAndGet();
                        }
                    }
                    try {
                        httpTookit.release();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
        
        int old_c = 0;
        while(true){
            if(count.get() == threadNum * countPerThread){
                
                System.out.println("total=" + threadNum * countPerThread +
                        " ,200=" + count_200+
                        " ,504=" + count_504+" ,503=" + count_503 +
                        " ,clientTimeOut="+count_clientTimeout +
                        " ,clientConnException="+count_clientConnException+ 
                        " ,other=" +count_other);
                

                break;
            }else{
                Thread.sleep(1000);
                int c = count.get();
                
                System.out.println("total="+c + " ,speed=" + (c-old_c) );
                old_c = c;
            }
        }
        
        
    }
    
}

